Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Run asynchronous commands synchronously by putting them in a separate process
The sync-rpc npm package allows for synchronous remote procedure calls (RPC) in Node.js. It enables the execution of functions in a separate process and returns the result synchronously, which can be useful for certain use cases where asynchronous behavior is not desired.
Synchronous RPC Call
This feature allows you to make synchronous RPC calls to a worker script. In this example, the worker script (worker.js) contains a function to add two numbers. The main script calls this function synchronously and logs the result.
const syncRpc = require('sync-rpc');
const rpc = syncRpc(__dirname + '/worker.js');
const result = rpc({ method: 'add', params: [1, 2] });
console.log(result); // Outputs: 3
Handling Multiple RPC Methods
This feature demonstrates how to handle multiple RPC methods within the worker script. The main script can call different methods (e.g., 'add' and 'subtract') and receive the results synchronously.
const syncRpc = require('sync-rpc');
const rpc = syncRpc(__dirname + '/worker.js');
const addResult = rpc({ method: 'add', params: [1, 2] });
const subtractResult = rpc({ method: 'subtract', params: [5, 3] });
console.log(addResult); // Outputs: 3
console.log(subtractResult); // Outputs: 2
Error Handling in RPC
This feature shows how to handle errors in synchronous RPC calls. If the worker script throws an error (e.g., division by zero), the main script can catch and handle the error appropriately.
const syncRpc = require('sync-rpc');
const rpc = syncRpc(__dirname + '/worker.js');
try {
const result = rpc({ method: 'divide', params: [1, 0] });
console.log(result);
} catch (error) {
console.error('Error:', error.message);
}
The node-ipc package provides a way to communicate between Node.js processes using Inter-Process Communication (IPC). Unlike sync-rpc, node-ipc is designed for asynchronous communication and supports various communication protocols such as TCP, UDP, and Unix Sockets.
zerorpc is a package that allows for RPC communication between different programming languages. It is built on top of ZeroMQ and MessagePack, providing a robust and language-agnostic RPC solution. Unlike sync-rpc, zerorpc supports both synchronous and asynchronous communication.
dnode is a package for asynchronous RPC in Node.js. It allows for remote method invocation between Node.js processes. Unlike sync-rpc, dnode is designed for asynchronous communication and provides a more flexible and dynamic way to define and call remote methods.
Run asynchronous commands synchronously by putting them in a separate process
npm install sync-rpc --save
function init(connection) {
// you can setup any connections you need here
return function (message) {
// Note how even though we return a promise, the resulting rpc client will be synchronous
return Promise.resolve('sent ' + message + ' to ' + connection);
}
}
module.exports = init;
const assert = require('assert');
const rpc = require('sync-rpc');
const client = rpc(__dirname + '/../test-worker.js', 'My Server');
const result = client('My Message');
assert(result === 'sent My Message to My Server');
MIT
FAQs
Run asynchronous commands synchronously by putting them in a separate process
The npm package sync-rpc receives a total of 737,438 weekly downloads. As such, sync-rpc popularity was classified as popular.
We found that sync-rpc demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.